<!doctype html>
<!--
Tests that WebXR doesn't update frame data when the tab is not focused
-->
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="../resources/webxr_e2e.css">
  </head>
  <body>
    <canvas id="webgl-canvas"></canvas>
    <script src="../../../../../../third_party/blink/web_tests/resources/testharness.js"></script>
    <script src="../resources/webxr_e2e.js"></script>
    <script src="../resources/webxr_boilerplate.js"></script>
    <script>
      setup({single_test: true});

      var pose = null;
      let counter = 0;

      function stepCheckFrameDataWhileFocusedTab() {
        function onAnimationFrame(session, frame) {
          // TODO(bsheedy): This is a workaround for crbug.com/787196. Either
          // remove the workaround once fixed or remove this todo.
          // Let several animation frames get triggered so we're sure to have a
          // pose
          if (counter <= 2) {
            counter++;
            return;
          }
          onMagicWindowXRFrameCallback = null;
          pose = frame.getViewerPose(sessionInfos[sessionTypes.MAGIC_WINDOW].currentRefSpace);
          assert_true(pose != null,
              "getViewerPose returned a non-null object");
          assert_true(pose instanceof XRViewerPose,
              "getViewerPose returned an XRViewerPose")
          finishJavaScriptStep();
        }
        // Make sure at least one rAF call has happened so we get valid data
        onMagicWindowXRFrameCallback = onAnimationFrame;
      }

      function stepCheckFrameDataWhileNonFocusedTab() {
        // Unlike WebVR, WebXR doesn't fire rAFs when in a different tab
        onMagicWindowXRFrameCallback = function() {
          assert_unreached("Magic window fired rAF while in a different tab");
        }
        window.setTimeout( () => {
          done();
        }, 1000);
      }
    </script>
  </body>
</html>
